/*
 * DataPemasokForm.java
 *
 * Created on November 10, 2011, 8:04 PM
 */
package Transaksi;

import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  Jo
 */
public class DataPemasokForm extends javax.swing.JPanel {

    /** Creates new form DataPemasokForm */
    public DataPemasokForm() {
        try {
            initComponents();
            simpanButton.setEnabled(false);
            hapusButton.setEnabled(false);
            ubahButton.setEnabled(false);
            
            List<Pemasok> pemasok = Pemasok.getKoneksi().getAllPemasok();
            PemasokTableModel model = new PemasokTableModel(pemasok);
            pemasokTabel.setModel(model);
        } catch (SQLException ex) {
            Logger.getLogger(DataPemasokForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        dataPemasokDesktopPane = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        dataPemasokTitle = new javax.swing.JLabel();
        namaPemasokLabel = new javax.swing.JLabel();
        alamatLabel = new javax.swing.JLabel();
        teleponLabel = new javax.swing.JLabel();
        personLabel = new javax.swing.JLabel();
        kodePemasokTextField = new javax.swing.JTextField();
        namaPemasokTextField = new javax.swing.JTextField();
        alamatTextField = new javax.swing.JTextField();
        teleponTextField = new javax.swing.JTextField();
        personTextField = new javax.swing.JTextField();
        ubahButton = new javax.swing.JButton();
        simpanButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        keluarButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        pemasokTabel = new javax.swing.JTable();
        kodePemasokLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));

        dataPemasokTitle.setFont(new java.awt.Font("Showcard Gothic", 0, 24));
        dataPemasokTitle.setText("Data pemasok ");

        namaPemasokLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        namaPemasokLabel.setText("Nama Pemasok");

        alamatLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        alamatLabel.setText("Alamat");

        teleponLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        teleponLabel.setText("Telepon");

        personLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        personLabel.setText("Person");

        kodePemasokTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                kodePemasokTextFieldKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                kodePemasokTextFieldKeyTyped(evt);
            }
        });

        namaPemasokTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                namaPemasokTextFieldKeyTyped(evt);
            }
        });

        alamatTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                alamatTextFieldKeyTyped(evt);
            }
        });

        teleponTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                teleponTextFieldKeyTyped(evt);
            }
        });

        personTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                personTextFieldKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                personTextFieldKeyTyped(evt);
            }
        });

        ubahButton.setText("Ubah");
        ubahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubahButtonActionPerformed(evt);
            }
        });

        simpanButton.setText("Simpan");
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });

        keluarButton.setText("Keluar");

        pemasokTabel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Kode Pemasok", "Nama Pemasok", "Alamat", "Telepon", "Person"
            }
        ));
        pemasokTabel.setGridColor(new java.awt.Color(0, 0, 0));
        pemasokTabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pemasokTabelMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(pemasokTabel);

        kodePemasokLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        kodePemasokLabel.setText("Kode Pemasok ");

        jLabel1.setText("Berisi detail data pemasok, berfungsi untuk menyimpan data pemasok baru ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dataPemasokTitle)
                .addContainerGap(305, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(129, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kodePemasokLabel)
                            .addComponent(namaPemasokLabel)
                            .addComponent(alamatLabel)
                            .addComponent(teleponLabel)
                            .addComponent(personLabel))
                        .addGap(66, 66, 66)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(personTextField)
                            .addComponent(teleponTextField)
                            .addComponent(alamatTextField)
                            .addComponent(namaPemasokTextField)
                            .addComponent(kodePemasokTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
                        .addGap(72, 72, 72)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(simpanButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ubahButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(keluarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(hapusButton, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(dataPemasokTitle)
                .addGap(4, 4, 4)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodePemasokLabel)
                    .addComponent(kodePemasokTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(simpanButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(namaPemasokLabel)
                    .addComponent(namaPemasokTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ubahButton))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(alamatTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alamatLabel)
                    .addComponent(hapusButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(teleponTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(teleponLabel)
                    .addComponent(keluarButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(personTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(personLabel))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(44, Short.MAX_VALUE))
        );

        jPanel1.setBounds(0, 0, 500, 370);
        dataPemasokDesktopPane.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(dataPemasokDesktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(dataPemasokDesktopPane, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed
        try {
            // TODO add your handling code here:
            Pemasok pemasokBaru = new Pemasok();
            String kode = kodePemasokTextField.getText();
            String nama = namaPemasokTextField.getText();
            String alamat = alamatTextField.getText();
            String telepon = teleponTextField.getText();
            String person = personTextField.getText();

            pemasokBaru.setKodePemasok(kode);
            pemasokBaru.setNamaPemasok(nama);
            pemasokBaru.setAlamat(alamat);
            pemasokBaru.setNoTelepon(telepon);
            pemasokBaru.setPerson(person);

            Pemasok.getKoneksi().tambahPemasokCallable(pemasokBaru);

            kodePemasokTextField.setText("");
            namaPemasokTextField.setText("");
            alamatTextField.setText("");
            teleponTextField.setText("");
            personTextField.setText("");

            List<Pemasok> pemasok = Pemasok.getKoneksi().getAllPemasok();
            PemasokTableModel model = new PemasokTableModel(pemasok);
            pemasokTabel.setModel(model);

            JOptionPane.showMessageDialog(null, "Data pemasok dengan kode " + kode + " berhasil ditambahkan");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Data pemasok gagal ditambahkan!");
            Logger.getLogger(DataPemasokForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_simpanButtonActionPerformed

    private void ubahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubahButtonActionPerformed
        try {
            // TODO add your handling code here:
            String kode = kodePemasokTextField.getText();
            String nama = namaPemasokTextField.getText();
            String alamat = alamatTextField.getText();
            String telepon = teleponTextField.getText();
            String person = personTextField.getText();

            Pemasok pemasokBaru = new Pemasok();
            pemasokBaru.setKodePemasok(kode);
            pemasokBaru.setNamaPemasok(nama);
            pemasokBaru.setAlamat(alamat);
            pemasokBaru.setNoTelepon(telepon);
            pemasokBaru.setPerson(person);

            Pemasok.getKoneksi().ubahPemasok(pemasokBaru);

            kodePemasokTextField.setText("");
            namaPemasokTextField.setText("");
            alamatTextField.setText("");
            teleponTextField.setText("");
            personTextField.setText(nama);
            hapusButton.setEnabled(false);
            ubahButton.setEnabled(false);

             List<Pemasok> pemasok = Pemasok.getKoneksi().getAllPemasok();
            PemasokTableModel model = new PemasokTableModel(pemasok);
            pemasokTabel.setModel(model);

            JOptionPane.showMessageDialog(null, "Data pemasok dengan kode " + kode + " berhasil diubah");

            kodePemasokTextField.setEditable(true);
            ubahButton.setEnabled(false);
            hapusButton.setEnabled(false);
        } catch (SQLException ex) {
            Logger.getLogger(DataPemasokForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Data pemasok gagal diedit!");
        }
    }//GEN-LAST:event_ubahButtonActionPerformed

    private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
        try {
            // TODO add your handling code here:
            String kode = kodePemasokTextField.getText();
            Pemasok pemasokBaru = new Pemasok();
            pemasokBaru.setKodePemasok(kode);

            Pemasok.getKoneksi().hapusPemasok(pemasokBaru);

            kodePemasokTextField.setText("");
            namaPemasokTextField.setText("");
            alamatTextField.setText("");
            teleponTextField.setText("");
            personTextField.setText("");

            List<Pemasok> pemasok = Pemasok.getKoneksi().getAllPemasok();
            PemasokTableModel model = new PemasokTableModel(pemasok);
            pemasokTabel.setModel(model);

            JOptionPane.showMessageDialog(null, "Data pemasok dengan kode " + kode + " berhasil dihapus");
            kodePemasokTextField.setEditable(true);
            hapusButton.setEnabled(false);
            ubahButton.setEnabled(false);
        } catch (SQLException ex) {
            Logger.getLogger(DataPemasokForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Data pemasok gagal dihapus!");
        }
    }//GEN-LAST:event_hapusButtonActionPerformed

    private void kodePemasokTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodePemasokTextFieldKeyReleased
        try {
            // TODO add your handling code here:
            String kode = kodePemasokTextField.getText();
            Pemasok pemasokBaru = new Pemasok();
            pemasokBaru.setKodePemasok(kode);
            Pemasok.getKoneksi().cariPemasok(pemasokBaru);

            String nama = pemasokBaru.getNamaPemasok();
            String alamat = pemasokBaru.getAlamat();
            String telepon = pemasokBaru.getNoTelepon();
            String person = pemasokBaru.getPerson();

            namaPemasokTextField.setText(nama);
            alamatTextField.setText(alamat);
            teleponTextField.setText(telepon);
            personTextField.setText(person);

        } catch (SQLException ex) {
            Logger.getLogger(DataPemasokForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_kodePemasokTextFieldKeyReleased

    private void personTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_personTextFieldKeyPressed
        // TODO add your handling code here:
       if (kodePemasokTextField.getText().isEmpty()
                || namaPemasokTextField.getText().isEmpty()
                || alamatTextField.getText().isEmpty()
                || teleponTextField.getText().isEmpty()
                || personTextField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Semua field harus diisi!");
            simpanButton.setEnabled(false);
        } else {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                simpanButton.setEnabled(true);
            }
        }

    }//GEN-LAST:event_personTextFieldKeyPressed

    private void kodePemasokTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodePemasokTextFieldKeyTyped
        // TODO add your handling code here:
        if (!Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }

    }//GEN-LAST:event_kodePemasokTextFieldKeyTyped

    private void teleponTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_teleponTextFieldKeyTyped
        // TODO add your handling code here:
        if (!Character.isDigit(evt.getKeyChar())) {
            evt.consume();

        }
    }//GEN-LAST:event_teleponTextFieldKeyTyped

    private void alamatTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_alamatTextFieldKeyTyped
        // TODO add your handling code here:
        if (Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }

    }//GEN-LAST:event_alamatTextFieldKeyTyped

    private void pemasokTabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pemasokTabelMouseClicked
        // TODO add your handling code here:
         simpanButton.setEnabled(false);
        ubahButton.setEnabled(true);
        hapusButton.setEnabled(true);
        int row = pemasokTabel.getSelectedRow();
        String kodePemasok = pemasokTabel.getValueAt(row, 0).toString();
        String namaPemasok = pemasokTabel.getValueAt(row, 1).toString();
        String alamat = pemasokTabel.getValueAt(row, 2).toString();
        String telepon = pemasokTabel.getValueAt(row, 3).toString();
        String person = pemasokTabel.getValueAt(row, 4).toString();

        kodePemasokTextField.setText(kodePemasok);
        namaPemasokTextField.setText(namaPemasok);
        alamatTextField.setText(alamat);
        teleponTextField.setText(telepon);
        personTextField.setText(person);

        kodePemasokTextField.setEditable(false);

         namaPemasokTextField.requestFocus();

    }//GEN-LAST:event_pemasokTabelMouseClicked

    private void namaPemasokTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_namaPemasokTextFieldKeyTyped
        // TODO add your handling code here:
        if (Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_namaPemasokTextFieldKeyTyped

    private void personTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_personTextFieldKeyTyped
        // TODO add your handling code here:
        if (Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_personTextFieldKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel alamatLabel;
    private javax.swing.JTextField alamatTextField;
    private javax.swing.JDesktopPane dataPemasokDesktopPane;
    private javax.swing.JLabel dataPemasokTitle;
    private javax.swing.JButton hapusButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton keluarButton;
    private javax.swing.JLabel kodePemasokLabel;
    private javax.swing.JTextField kodePemasokTextField;
    private javax.swing.JLabel namaPemasokLabel;
    private javax.swing.JTextField namaPemasokTextField;
    private javax.swing.JTable pemasokTabel;
    private javax.swing.JLabel personLabel;
    private javax.swing.JTextField personTextField;
    private javax.swing.JButton simpanButton;
    private javax.swing.JLabel teleponLabel;
    private javax.swing.JTextField teleponTextField;
    private javax.swing.JButton ubahButton;
    // End of variables declaration//GEN-END:variables
}
